package stream;

import com.sun.org.apache.xpath.internal.operations.String;

import java.util.*;
import java.util.stream.Collectors;

public class StreamApi {
    public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        User users = null;
        for (int i = 0; i < 2; i++) {
            users = new User().setSex("男").setUsername("流派:"+i).setAge(22 + i);
            list.add(users);
        }
        User u = new User().setSex("男").setUsername("流派:1").setAge(4444);
        list.add(u);
        List<Integer> integerList = new ArrayList<>();
        integerList.add(4444);
        List<User> userList = list.stream().filter(user -> !integerList.contains(user.getAge())).collect(Collectors.toList());
        System.out.println(userList);
//        List<String> stringList = list.stream().filter(user -> user.getAge() < 25).sorted(Comparator.comparing(User::getAge))
//                .map(User::getUsername) //提取出名字(只展示出名字)
//                .collect(Collectors.toList());
//        System.out.println(stringList);
//
//        //筛选出年龄为23岁的
//        List<User> collect = list.stream().filter(user -> user.getAge() == 23).collect(Collectors.toList());
//        System.out.println(collect);
//        //筛选出年龄为25,性别为女的用户
//        List<User> userList = list.stream().filter(user -> user.getAge() == 25 && user.getSex().equals("女")).collect(Collectors.toList());
//        System.out.println(userList);
//        //排序
//        List<User> collect1 = list.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
//        System.out.println(collect1);
//        //将几个根据某个值作为key,转换为map
//        Map<String, List<User>> collect2 = list.stream().collect(groupingBy(User::getUsername));
//        System.out.println(collect2);
//        //去重
//        List<User> collect3 = list.stream().distinct().collect(Collectors.toList());
//        System.out.println(collect3);
//        //限制返回条数
//        List<User> limit = list.stream().limit(2).collect(Collectors.toList());
//        System.out.println(limit);
//        //获取最大值
//        Optional<Integer> max = list.stream().map(User::getAge).max(Integer::compareTo);
//        System.out.println(max.get());
//        Optional<Integer> min = list.stream().map(User::getAge).min(Integer::compareTo);
//        System.out.println(min.get());
//        //求和（包含总数，和，最小值，最大值，平均值）
//        IntSummaryStatistics collect = list.stream().collect(summarizingInt(User::getAge));
//        System.out.println(collect);//IntSummaryStatistics{count=11, sum=288, min=22, average=26.181818, max=31}
        //求和
//        int sum = list.stream().mapToInt(User::getAge).sum();
//        System.out.println(sum);
//        Integer reduce = list.stream().map(User::getAge).reduce(0, Integer::sum);
//        System.out.println(reduce);
        //求平均值
//        Double collect = list.stream().collect(averagingInt(User::getAge));
//        System.out.println(collect);

//        List<User> userList = list.stream().distinct().filter(user -> user.getSex().equals("男") && user.getAge() <= 30 && user.getAge() >= 23)
//                .collect(toList());
//        System.out.println(userList);
    }
}
